package models

import "gorm.io/gorm"

type House struct {
	gorm.Model
	UserId    int     `gorm:"type:int(11);not null;comment:用户ID"`
	Title     string  `gorm:"type:varchar(30);not null;comment:房屋名称"`
	Image     string  `gorm:"type:varchar(500);not null;comment:房屋封面"`
	Price     int     `gorm:"type:int(11);not null;comment:价格"`
	Fool      float64 `gorm:"type:decimal(10,2);not null;comment:面积"`
	HouseType string  `gorm:"type:varchar(20);not null;comment:类型"`
	Status    int     `gorm:"type:tinyint;not null;comment:状态 0:审核中 1:审核通过 2:审核不通过 3:下架"`
}

func (h *House) AddHouse() error {
	return DB.Debug().Create(&h).Error
}

func (h *House) ShowHouseByUserId() error {
	return DB.Debug().Where("user_id = ?", h.UserId).Find(&h).Error
}

func (h *House) ShowHouse() error {
	return DB.Debug().Where("id = ?", h.ID).Find(&h).Error
}

func (h *House) UpdateHouse() error {
	return DB.Debug().Updates(&h).Error
}

func (h *House) ListHouse() (house []*House, err error) {
	err = DB.Debug().Order("created_at desc").Find(&house).Error
	return
}

func (h *House) ListHouseByPrice(min, max int) (house []*House, err error) {
	err = DB.Debug().Where("price => ? && price<= ?", min, max).Find(&house).Error
	return
}

type LookHouse struct {
	gorm.Model
	UserId  int    `gorm:"type:int(11);not null;comment:用户ID"`
	HouseId int    `gorm:"type:int(11);not null;comment:房源ID"`
	Time    string `gorm:"type:varchar(30);not null;comment:预约看房时间"`
	Comment string `gorm:"type:varchar(100);comment:评论反馈"`
	Status  int    `gorm:"type:tinyint;not null;comment:状态 0:预约成功 1:预约取消 2:已看房"`
}

func (h *LookHouse) CreateOrder() error {
	return DB.Debug().Create(&h).Error
}

func (h *LookHouse) ShowOrder() error {
	return DB.Debug().Where("user_id = ?&&house_id = ?", h.UserId, h.HouseId).Find(&h).Error
}

func (h *LookHouse) UpdateOrder() error {
	return DB.Debug().Updates(&h).Error
}

func (h *LookHouse) ListOrder() (list []*LookHouse, err error) {
	err = DB.Debug().Find(&h).Error
	return
}

func (h *LookHouse) ListLookHouseByUserId() (list []*LookHouse, err error) {
	err = DB.Debug().Where("user_id = ?", h.UserId).Find(&h).Error
	return
}
